--[[
	FONTE: 
		* https://github.com/BlockMen/hud
		* https://github.com/BlockMen/hud/blob/master/init.lua
]]--

lunogauges.huds.xp_player = {}
lunogauges.huds.gaugexp = {}

lunogauges.huds.add_xp = function(player)
	if player~= nil and player:is_player() then
		lunogauges.huds.gaugexp[player:get_player_name()] = {}

		local Xp = lunomods.getExperience(player:get_player_name())
		local Nv = lunomods.getLevel(player:get_player_name())
		local XPmax = lunomods.getExperienciaPorNivel(Nv+1)
		local XPmin = lunomods.getExperienciaPorNivel(Nv)
		local percentual = math.floor((((XPmax - XPmin)-(Xp - XPmin))/(XPmax - XPmin)) * 100)
				
		lunogauges.huds.gaugexp[player:get_player_name()].imgPainel = player:hud_add({
			hud_elem_type = "image",
			name = "MailIcon",
			position = {x=0.65, y=0.033},
			--position = {x=0.65, y=0.015},
			--position = {x=0.92, y=0.015},
			text="hud_menu_status.png",
			scale = {x=1,y=1},
			alignment = {x=0, y=0},
		})
		lunogauges.huds.gaugexp[player:get_player_name()].lblNome = player:hud_add({
			hud_elem_type = "text",
			name = "MailText",
			--position = {x=0.58, y=0.72},
			position = {x=0.655, y=0.024},
			--text="Agora: "..os.date("%Y-%m-%d %H:%M:%S"),
			--text= player:get_player_name().." - Nivel:"..Nv.." ("..Xp.."Xp)",
			text=player:get_player_name(),
			scale = {x=1,y=1},
			alignment = {x=0, y=0},
		})
		lunogauges.huds.gaugexp[player:get_player_name()].lblNivel = player:hud_add({
			hud_elem_type = "text",
			name = "MailText",
			--position = {x=0.58, y=0.72},
			position = {x=0.567, y=0.051},
			--text="Agora: "..os.date("%Y-%m-%d %H:%M:%S"),
			--text= "Nivel:"..Nv.." ("..Xp.."Xp)",
			text=Nv,
			scale = {x=0.5,y=0.5},
			alignment = {x=0, y=0},
		})
		lunogauges.huds.gaugexp[player:get_player_name()].lblXP = player:hud_add({
			hud_elem_type = "text",
			name = "MailText",
			--position = {x=0.58, y=0.72},
			position = {x=0.655, y=0.051},
			--text="Agora: "..os.date("%Y-%m-%d %H:%M:%S"),
			--text= "Nivel:"..Nv.." ("..Xp.."Xp)",
			text=Xp.."xp (+"..percentual.."%)",
			scale = {x=0.5,y=0.5},
			alignment = {x=0, y=0},
		})

		lunogauges.huds.xp_player[player:get_player_name()] = Xp
	end
end

lunogauges.huds.update_xp= function(player)
	if player~= nil and player:is_player() then
		local name = player:get_player_name()
		
		local Xp = lunomods.getCharValue(player:get_player_name(), "Experience")
		if Xp==nil or Xp=="" or type(Xp)~="number" or (type(Xp)=="number" and Xp<0) then 
			Xp=0 
		else
			Xp = math.floor(Xp)
		end
		local Nv = math.floor(1 + math.sqrt(Xp/100)) --Nv == Nível
		local XPmax = lunomods.getExperienciaPorNivel(Nv+1)
		local XPmin = lunomods.getExperienciaPorNivel(Nv)
		local percentual = math.floor((((XPmax - XPmin)-(Xp - XPmin))/(XPmax - XPmin)) * 100)

		if lunogauges.huds.xp_player[name]== nil or lunogauges.huds.xp_player[name] ~= Xp   then
			lunogauges.huds.xp_player[name] = Xp
			if lunogauges.huds.gaugexp[name]~=nil and lunogauges.huds.gaugexp[name].lblNivel~=nil and lunogauges.huds.gaugexp[name].lblXP~=nil then
				player:hud_change(lunogauges.huds.gaugexp[name].lblNivel, "text", Nv)
				player:hud_change(lunogauges.huds.gaugexp[name].lblXP,	 "text", Xp.."xp (+"..percentual.."%)")
			else
				lunogauges.huds.add_xp(player)
			end
		end
	end
end


